package com.javaclimb.music.controller;

import com.javaclimb.music.domain.SongList;
import com.javaclimb.music.service.SongListService;
import com.javaclimb.music.utils.PageUtils;
import com.javaclimb.music.utils.R;
import com.javaclimb.music.vo.SongListVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;

/**
 * @author 夏天
 * @date 2020年11月24日 9:45
 * 歌单控制类
 */

@RestController
@RequestMapping("/songList")
@Slf4j
@Api(tags = "歌单")
public class SongListController {
    @Autowired
    SongListService songListService;

    @ApiOperation("修改或添加歌单")
    @PostMapping("/saveOrUpdate")
    public R addSongList(@RequestBody SongListVo songListVo) {
        if (StringUtils.isEmpty(songListVo.getPic())) {
            //默认图片
            songListVo.setPic("/img/songListPic/tubiao.jpg");
        }
        SongList songList = new SongList();
        BeanUtils.copyProperties(songListVo, songList);
        if (songListService.saveOrUpdate(songList)) {
            return R.ok();
        }
        return R.error("添加或保存歌单失败");
    }

    @ApiOperation("删除歌单")
    @PostMapping("/delete")
    public R deleteSongList(@RequestBody Integer[] ids) {
        if (songListService.removeByIds(Arrays.asList(ids))) {
            return R.ok();
        }
        return R.error("删除歌单失败");
    }

    @ApiOperation("获取歌单信息")
    @GetMapping("/info/{id}")
    public R getSongList(@PathVariable("id") Integer id) {
        SongList songList = songListService.getById(id);
        if (songList != null) {
            return R.ok().put("info", songList);
        }
        return R.error("获取歌单信息失败");
    }

    @ApiOperation("获取所以歌单信息")
    @GetMapping("/list")
    public R listSongList(@RequestParam Map<String, Object> params) {
        PageUtils pageUtils = songListService.queryPage(params);
        return R.ok().put("data", pageUtils);
    }

    @ApiOperation("更新歌单图片")
    @PostMapping("/updateSongListPic")
    public R updateSongListPic(@RequestParam("file") MultipartFile avatarFile, @RequestParam("id") int id) {
        if (avatarFile == null || avatarFile.isEmpty()) {
            return R.error("图片上传失败");
        }
        //文件名=当前时间戳+源文件名
        String fileName = System.currentTimeMillis() + avatarFile.getOriginalFilename();
        //文件路径
        String filePath = System.getProperty("user.dir") + System.getProperty("file.separator")
                + "img" + System.getProperty("file.separator")
                + "songListPic";
        //如果路径不存在则创建文件夹
        File file = new File(filePath);
        if (!file.exists()) {
            file.mkdirs();
        }
        //实际文件的地址
        File dest = new File(filePath + System.getProperty("file.separator") + fileName);
        //存储到数据库里的相对稳健地址
        String storeAvatarPath = "/img/songListPic/" + fileName;
        try {
            avatarFile.transferTo(dest);
            SongList songList = new SongList();
            songList.setId(id);
            songList.setPic(storeAvatarPath);
            if (songListService.updateById(songList)) {
                return R.ok("上传成功");
            }
        } catch (IOException e) {
            log.error("上传图片失败", e);
            return R.error("上传失败");
        }
        log.error("上传图片失败：可能是修改数据表图片信息出现问题");
        return R.error("修改失败");
    }


}
